﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagGroup.Base.UI;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using Application = System.Windows.Application;

namespace HomagChina.Nesting
{
    public class PartDetailsDialogViewModel : NotifyPropertyChangedBase
    {
        public IDictionary<int, Brush> CustomStatus { get; set; }
        private SearchInfoHandle Handle_Search;
        private DeleteInfoHandle Handle_Delete;
        private ObservableCollection<LabelTaskListModel> _List_PartModel;

        public ObservableCollection<LabelTaskListModel> List_PartModel
        {
            get => _List_PartModel;
            set { _List_PartModel = value; OnPropertyChanged(); }
        }

        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }

        public RelayCommand HandlePrint { get; set; }

        public RelayCommand HandleDeletePrint { get; set; }
        private string _partId;

        public string PartId
        { get => _partId; set { _partId = value; OnPropertyChanged(); } }

        private int _Id;

        public int Id
        {
            get => _Id;
            set { _Id = value; OnPropertyChanged(); }
        }
        private string _MprInfo;

        public string MprInfo
        {
            get => _MprInfo;
            set { _MprInfo = value; OnPropertyChanged(); }
        }
        private static DispatcherTimer timer;

        public PartDetailsDialogViewModel(int BoardInfpId)
        {
            Handle_Delete = new DeleteInfoHandle();
            Handle_Search = new SearchInfoHandle();
            LoadedCommand = new RelayCommand(ViewloadedCommand);
            UnLoadedCommand = new RelayCommand(ViewUnloadedCommand);
            Id = BoardInfpId;
            CustomStatus = new Dictionary<int, Brush>
        {
            { 0, Application.Current.TryFindResource(HomagGroup.Base.UI.Themes.Resources.Keys.MoreImage) as Brush },
            { 4, Application.Current.TryFindResource(HomagGroup.Base.UI.Themes.Resources.InternalKeys.TableActualStateImage) as Brush },
            { 6, Application.Current.TryFindResource(HomagGroup.Base.UI.Themes.Resources.InternalKeys.TableFinishedStateImage) as Brush },
         };

            timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(3000)
            };
        }

        private void ViewloadedCommand()
        {
            try
            {
                List_PartModel = Handle_Search.GetMprDetailsByMprName(Id);
                MprInfo = List_PartModel[0].MprName;
                Refresh();
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// Unloaded
        /// </summary>
        private void ViewUnloadedCommand()
        {
            timer.Stop();
        }

        public event EventHandler RequestScrollPositionSave;

        public event EventHandler RequestScrollPositionRestore;

        public void Refresh()
        {
            timer.Tick += (sender, args) =>
            {
                // 通知 View 保存滚动位置
                RequestScrollPositionSave?.Invoke(this, EventArgs.Empty);
                if (List_PartModel != null) { List_PartModel.Clear(); }
                List_PartModel = Handle_Search.GetMprDetailsByMprName(Id);
                MprInfo = List_PartModel[0].MprName;

                // 通知 View 恢复滚动位置
                RequestScrollPositionRestore?.Invoke(this, EventArgs.Empty);
            };
            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                timer.Start();
            }
        }
    }
}